/**
 * This component allow to register a new question.
 */
$.Controller( "AdminQuestionList", {

	questions: [],

	init: function() {
		var me = this;
		
		Question.findAll({}, function(questions) {
			me.questions = questions;
			me.element.html("js/view/AdminQuestionList.ejs", {questions: me.questions});
		});
	},
  
	".remove-question click": function(el) {
		var me = this,
			removeIndex=-1;
			removeQuestion = null;
		
		$.each(me.questions, function (index, question){
			if (question.attr('id')==el.context.attributes.role.value) {
				removeIndex = index;
				removeQuestion = question;
				return false;
			}
		});
		
		if (removeIndex >= 0) {			
			removeQuestion.destroy(function() {
				me.questions.splice(removeIndex, 1);
				me.element.html("js/view/AdminQuestionList.ejs", {questions: me.questions});
				
			});
		}
		
	}
});
